package cn.shop.admin.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.digest.DigestUtil;
import cn.shop.admin.service.MerchantRateService;
import cn.shop.admin.service.MerchantUserService;
import cn.shop.transcation.backRate.entity.BackRate;
import cn.shop.transcation.backRate.mapper.BackRateMapper;
import cn.shop.transcation.merchantRateModule.entity.MerchantRate;
import cn.shop.transcation.merchantUser.mapper.MerchantUserMapper;
import cn.shop.transcation.merchantUser.mapper.entity.MerchantUser;
import cn.shop.utils.constant.Crypto;
import cn.shop.utils.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 商户用户表(MerchantUser)表服务实现类
 *
 * @author jzw
 * @since 2021-11-04 17:17:39
 */
@Service("merchantUserService")
public class MerchantUserServiceImpl extends ServiceImpl<MerchantUserMapper, MerchantUser> implements MerchantUserService {

	@Resource
	private BackRateMapper backRateMapper;

	@Resource
	private MerchantRateService merchantRateService;

	/**
	 * 批量删除数据
	 *
	 * @param t      实体类
	 * @param idList id 集合
	 * @return 执行结果
	 * @author jzw
	 * @since 2021/11/15 14:05
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean batchDelete(List<Long> idList, MerchantUser t) {
		return deleteWithFill(t, new LambdaQueryWrapper<>(t).in(MerchantUser::getId, idList));
	}

	/**
	 * 删除数据
	 *
	 * @param t       实体类
	 * @param wrapper 删除条件
	 * @return 执行结果
	 * @author jzw
	 * @since 2021/11/15 14:05
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteWithFill(MerchantUser t, Wrapper<MerchantUser> wrapper) {
		return SqlHelper.retBool(baseMapper.batchDeleteWithFill(t, wrapper));
	}

	/**
	 * 注册
	 *
	 * @param merchantUser 实体对象
	 * @return 新增结果
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean register(MerchantUser merchantUser) {
		LambdaQueryWrapper<MerchantUser> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(MerchantUser::getAccount, merchantUser.getAccount());
		MerchantUser user = baseMapper.selectOne(wrapper);
		Assert.isNull(user, "当前账户已存在");
		LambdaQueryWrapper<BackRate> rateQuery = Wrappers.lambdaQuery();
		rateQuery.eq(BackRate::getType, "A");

		String password = merchantUser.getPassword();
		merchantUser.setPassword(DigestUtil.bcrypt(password));
		int insert = baseMapper.insert(merchantUser);
		// 给商户添加默认费率
		BackRate currencyRate = backRateMapper.selectOne(rateQuery);
		MerchantRate merchantRate = new MerchantRate();
		merchantRate.setChargeSingle(currencyRate.getChargeSingle());
		merchantRate.setChargeRate(currencyRate.getChargeRate());
		merchantRate.setSingleType(currencyRate.getSingleType());
		merchantRate.setRateType(currencyRate.getRateType());
		merchantRate.setTemplateFloat(currencyRate.getTemplateFloat());
		merchantRate.setId(merchantUser.getId());
		merchantRateService.save(merchantRate);
		// 给商户添加默认角色
		baseMapper.setRole(merchantUser.getId());
		return SqlHelper.retBool(insert);
	}

	/**
	 * 修改数据
	 *
	 * @param merchantUser 实体对象
	 * @return 修改结果
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean update(MerchantUser merchantUser) {
		LambdaQueryWrapper<MerchantUser> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(MerchantUser::getAccount, merchantUser.getAccount()).ne(MerchantUser::getId, merchantUser.getId());
		MerchantUser user = baseMapper.selectOne(wrapper);
		Assert.isNull(user, "当前账户已存在");
		String password = merchantUser.getPassword();
		if (StrUtil.isNotBlank(password)) {
			merchantUser.setPassword(DigestUtil.bcrypt(password));
		}
		return SqlHelper.retBool(baseMapper.updateById(merchantUser));
	}

	/**
	 * 获取商户加密信息
	 *
	 * @param merchantUser
	 * @return
	 * @author jzw
	 * @since 2022/2/24 14:04
	 */
	@Override
	public Map<String, String> merchantSystemCode(MerchantUser merchantUser) {
		merchantUser = baseMapper.selectById(merchantUser);
		if (merchantUser != null) {
			RSA rsa = SecureUtil.rsa(null, Crypto.PUBLIC_KEY);
			String code = rsa.encryptHex(merchantUser.getPassword() + ',' + merchantUser.getAccount(), KeyType.PublicKey);
			return Collections.singletonMap("code", code);
		}
		throw new ServiceException("商户信息不正确");
	}

	/**
	 * 获取商户的游戏名称
	 *
	 * @param id 商户id
	 * @return
	 * @author jzw
	 * @since 2022/2/26 15:52
	 */
	@Override
	public List<Map<String, String>> getGameName(Long id) {
		return baseMapper.getGameName(id);
	}

	@Override
	public List<Map<String, String>> getAliasName(Long id) {
		return baseMapper.getAlias(id);
	}

	@Override
	public boolean auditAlias(Long id, Long state) {
		return SqlHelper.retBool(baseMapper.auditAlias(id, state));
	}

	/**
	 * 修改费率数据
	 *
	 * @param merchantRate
	 * @return boolean
	 * @author 痴
	 * @since 2022/5/12 16:13
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateRate(MerchantRate merchantRate) {
		// MerchantUser oldMerchant = baseMapper.selectById(merchantRate.getId());
		LambdaUpdateWrapper<MerchantUser> merchantUserLambdaUpdateWrapper = Wrappers.lambdaUpdate();
		merchantUserLambdaUpdateWrapper.
				eq(MerchantUser::getId, merchantRate.getId());
		return SqlHelper.retBool(baseMapper.update(new MerchantUser(), merchantUserLambdaUpdateWrapper));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean audit(MerchantUser merchantUser) {
		// 给商户添加默认费率
		LambdaQueryWrapper<BackRate> rateQuery = Wrappers.lambdaQuery();
		rateQuery.eq(BackRate::getType, "A");
		BackRate currencyRate = backRateMapper.selectOne(rateQuery);
		MerchantRate merchantRate = new MerchantRate();
		merchantRate.setChargeSingle(currencyRate.getChargeSingle());
		merchantRate.setChargeRate(currencyRate.getChargeRate());
		merchantRate.setSingleType(currencyRate.getSingleType());
		merchantRate.setRateType(currencyRate.getRateType());
		merchantRate.setTemplateFloat(currencyRate.getTemplateFloat());
		merchantRate.setId(merchantUser.getId());
		merchantRateService.saveOrUpdate(merchantRate);
		return SqlHelper.retBool(baseMapper.updateById(merchantUser));
	}

}

